#include "StdAfx.h"
#include "../sgp/VideoSurfaceHandle.h"
#include "DXCommon.h"
#include "..\sgp\VideoSurfaceLock.h"

namespace ja2{ namespace sgp
{
	VideoSurfaceLock::VideoSurfaceLock(IDirectDrawSurfacePtr &pSurface)
		: m_pSurface(pSurface)
	{
		// Lock whole surface
		DDSURFACEDESC2 surface_description = DDSURFACEDESC2();
		surface_description.dwSize = sizeof(surface_description);
		dx_attempt(m_pSurface->Lock(NULL, &surface_description, 0, NULL));

		std::get<0>(m_Data) = surface_description.lpSurface;
		std::get<1>(m_Data) = surface_description.lPitch;
	}

	VideoSurfaceLock::VideoSurfaceLock(VideoSurfaceLock &&Move)
		: m_pSurface(Move.m_pSurface), m_Data(std::move(Move.m_Data))
	{
	}
	
	VideoSurfaceLock::~VideoSurfaceLock()
	{
		try
		{
			// Unlock whole DD surface
			m_pSurface->Unlock(NULL);
		}
		catch(std::exception const &e)
		{
			JA2_FAST_DEBUG_MESSAGE(("Exception in ~VideoSurfaceLock(): " + std::string(e.what())).c_str());
		}
	}

	void *VideoSurfaceLock::buffer() const
	{
		return std::get<0>(m_Data);
	}

	std::uint_least32_t VideoSurfaceLock::pitch() const
	{
		return std::get<1>(m_Data);
	}
} /*sgp*/} /*ja2*/